package com.jsle.financial.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jsle.common.dao.mybatis.BaseDao;
import com.jsle.financial.domain.Company;
import com.jsle.financial.domain.Company_Resources;
import com.jsle.financial.domain.User_UserGroups;
import com.jsle.financial.util.Constants;
import com.jsle.financial.vo.Page;
import com.jsle.financial.vo.PageAndParameters;
import com.jsle.financial.vo.Result;


@SuppressWarnings("unchecked")
public class CompanyDaoImpl extends BaseDao implements CompanyDao{
	private Log log = LogFactory.getLog(CompanyDaoImpl.class);
	/**
	 * 新增一公司
	 * @param user
	 * @return
	 */
	public Result addCompany(Company company){
		Result result = new Result();
		try {
			boolean ret = insert("Company.insertCompany", company);
			Company tempcompany = null;
			if(ret){
				tempcompany = getCompanyByCcode(company);
				for (Company_Resources cr : company.getCrs()) {
					cr.setCompanyId(tempcompany.getId());
					insert("CompanyResource.insertCompanyResource", cr);
				}
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			log.error("new company error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 删除一公司
	 * @param user
	 * @return
	 */
	public Result delCompany(Company company){
		Result result = new Result();
		try {
			boolean ret = delete("Company.delCompany",company);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "User"));
			log.error("del company error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 更新一公司信息
	 * @param user
	 * @return
	 */
	public Result updateCompany(Company company){
		Result result = new Result();
		try {
			boolean ret = update("Company.updateCompany",company);
			boolean ret1 = delete("Company.delCompany_resource",company);
			for (Company_Resources cr : company.getCrs()) {
				insert("CompanyResource.insertCompanyResource", cr);
			}
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "User"));
			log.error("update company error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据公司代码获取一公司信息
	 * @param loginName
	 * @return
	 */
	public Result queryCompanyByCompanyCode(Company companyParameter){
		Result result = new Result();
		try {
			Company compangy = (Company)queryCountForObject("Company.selCompany",companyParameter);
			if(compangy!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(compangy);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			log.error("query company error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据公司代码获取一公司信息
	 * @param loginName
	 * @return
	 */
	public Result queryCompanyByCompanyID(Company companyParameter){
		Result result = new Result();
		try {
			Company company = (Company)queryCountForObject("Company.selCompanyByid",companyParameter);
			if(company!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(company);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			log.error("query company error## "+ e.getMessage());
		}
		return result;
	}
	
	public List<Company> selCompanysByPage(Page page){
		return null;
	}
	/**
	 * 公司配置资源
	 * @param cr
	 * @return
	 */
	public Result addCompanyAndResources(Company_Resources cr) {
		Result result = new Result();
		try {
			boolean ret = insert("CompanyResource.insertCompanyResource",cr);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "User"));
			log.error("add Company_Resources error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 *分页查询
	 */
	public Result queryCompanysByPage(Page page, String companyCode) {
		Result result = new Result();
		PageAndParameters param = new PageAndParameters(page.getPageIndex(),page.getPageSize(),companyCode);
		try {
			List<Company> cps = queryForList("Company.selCompanys", param);
			Company company = new Company();
			company.setCompanyCode(companyCode);
			int totalCount = (Integer)queryCountForObject("Company.selCompanysCounts", company);
			if (totalCount < 1){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(new Page());
				return result;
			}
				

			int startIndex = Page.getStartOfPage(page.getPageIndex(), page.getPageSize());
			
			if(cps!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(new Page(startIndex, totalCount, page.getPageSize(), cps));
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "CompanysByPage"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "CompanysByPage"));
			log.error("query companys by page error## "+ e.getMessage());
		}
		return result;
	}
	
	public Company getCompanyByCcode(Company company){
		return (Company) queryForObject("Company.selectByCode", company);
	}
	
	/**
	 * 查询某个公司配置的资源关系
	 * @param uug
	 * @return
	 */
	public Result getCompanyByCid(Company company){
		Result result = new Result();
		try {
			List<Company_Resources> crs = queryForList("CompanyResource.queryCompanyResourcesByCompanyid",company);
			if(crs!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(crs);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "getCompanyByCid"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "getCompanyByCid"));
			log.error("getCompanyByCid error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据公司code删除人员信息
	 * @param company
	 * @return
	 */
	public Result delUsersByCompany(Company company){
		Result result = new Result();
		try {
			boolean ret = delete("Company.delUsersByCompany",company);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "delUsersByCompany"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "delUsersByCompany"));
			log.error("delUsersByCompany error## "+ e.getMessage());
		}
		return result;
	}
	 
	/**
	 * 根据公司code删除用户组信息
	 * @param company
	 * @return
	 */
	public Result delUserGroupsByCompany(Company company){
		Result result = new Result();
		try {
			boolean ret = delete("Company.delUserGroupsByCompany",company);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "delUserGroupsByCompany"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "delUserGroupsByCompany"));
			log.error("delUserGroupsByCompany error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据公司code删除公司关联资源信息
	 * @param company
	 * @return
	 */
	public Result delCompanyResourcesByCompany(Company company){
		Result result = new Result();
		try {
			boolean ret = delete("Company.delCompanyResourcesByCompany",company);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "delCompanyResourcesByCompany"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "delCompanyResourcesByCompany"));
			log.error("delCompanyResourcesByCompany error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据公司code删除人员-用户组关联信息
	 * @param company
	 * @return
	 */
	public Result delUserUserGroupsByCompany(Company company){
		Result result = new Result();
		try {
			boolean ret = delete("Company.delUserUserGroupsByCompany",company);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "delUserUserGroupsByCompany"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "delUserUserGroupsByCompany"));
			log.error("delUserUserGroupsByCompany error## "+ e.getMessage());
		}
		return result;
	}
	public List<Company_Resources> getCompanyRes(Company company) {
		// TODO Auto-generated method stub
		return null;
	}
	public Result queryCompanys() {
		Result result = new Result();
		try {
			List<Company> companys = queryForList("Company.selAllCompanys");
			if((companys!=null)&&(companys.size()>0)){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(companys);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "queryCompanys"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "queryCompanys"));
			log.error("queryCompanys error## "+ e.getMessage());
		}
		return result;
	}
}
